<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="68px"
      class="globalSerchFormStyle"
    >
      <el-row>
        <el-col>
          <div style="margin-right: 20px; font-size: 14px">
            <span>订单数：</span>
            <label>{{ !lngData.countNum ? 0 : lngData.countNum }}</label>
          </div>
          <div style="font-size: 14px">
            <span>金额：</span>
            <label>{{ !lngData.amt ? 0 : lngData.amt }}</label>
          </div>
          <!-- <el-form-item label="订单数：" prop="orderCount">
            <label>{{ !lngData.countNum ? 0 : lngData.countNum }}</label>
          </el-form-item> -->
          <!-- <el-form-item label="金额：" prop="orderAmt">
            <label>{{ !lngData.amt ? 0 : lngData.amt }}</label>
          </el-form-item> -->
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <el-form-item label="订单号" prop="orderNo">
            <el-input
              v-model="queryParams.orderNo"
              placeholder="请输入订单号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
              style="width: 190px"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="创建时间" prop="createdDate">
            <el-date-picker
              style="width: 190px"
              v-model="dateRangeCreatedDate"
              size="small"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="datetimerange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <!-- <el-col :span="6">
          <el-form-item label="客户姓名" prop="likeConsumer">
            <el-input
              v-model="queryParams.likeConsumer"
              placeholder="请输入客户姓名"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
               style="width: 190px"
            />
          </el-form-item>
        </el-col> -->
      </el-row>
    </el-form>
    <!-- 功能按钮 -->
    <el-row class="global_el_rowStyle" :gutter="10">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
          plain
          >重置</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          >导出</el-button
        >
      </el-col>
    </el-row>
    <!-- 表格 -->
    <el-table :data="orderList" stripe>
      <af-table-column
        label="油站名"
        fixed
        align="left"
        prop="stationName"
        v-if="jiBie == 0 || jiBie == 1"
      />
      <af-table-column
        label="订单id"
        align="left"
        prop="orderId"
        v-if="false"
      />
      <af-table-column label="订单号" align="left" prop="orderNo" />
      <af-table-column label="油品名称" align="left" prop="oilName" />

      <af-table-column label="枪标价（元）" align="left" prop="oilPirce" />
      <af-table-column label="结算价（元）" align="left" prop="sellOilPrice" />
      <af-table-column
        label="司机价（元）"
        align="left"
        prop="driverOilPrice"
        v-if="jiBie == 0 || jiBie == 1"
      />

      <af-table-column label="数量(L/kg)" align="left" prop="orderLiters" />
      <af-table-column
        label="应付金额（元）"
        align="left"
        prop="receivableAmt"
      />
      <af-table-column label="结算金额（元）" align="left" prop="sellAmt" />
      <af-table-column
        label="结算优惠金额（元）"
        align="left"
        prop="sellDiscountAmt"
      />
      <af-table-column
        label="司机金额（元）"
        align="left"
        prop="amt"
        v-if="jiBie == 0 || jiBie == 1"
      />
      <af-table-column
        label="司机优惠金额（元）"
        align="left"
        prop="driverDiscountAmt"
        v-if="jiBie == 0 || jiBie == 1"
      />
      <af-table-column
        label="总优惠金额（元）"
        align="left"
        prop="discountAmt"
        v-if="jiBie == 0 || jiBie == 1"
      />
      <!-- <af-table-column label="客户姓名" align="left" prop="consumer" /> -->
      <af-table-column label="手机号" align="left" prop="mobilePhone" />
      <!-- <af-table-column label="加油员" align="left" prop="oilPersonnel" /> -->
      <!-- <af-table-column label="加油枪号" align="left" prop="oilGun" /> -->
      <af-table-column label="支付时间" align="left" prop="payDate">
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.payDate, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </af-table-column>

      <af-table-column
        label="支付状态"
        align="left"
        prop="status"
        :formatter="statusFotmat"
      >
      </af-table-column>

      <el-table-column
        label="操作"
        align="left"
        fixed="right"
        width="120"
        prop="payDate"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handlerefund(scope.row)"
            >退款</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handlereback(scope.row)"
            >回退</el-button
          >
        </template>
      </el-table-column>

      <!-- <af-table-column label="创建时间" align="left" prop="createdDate">
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.createdDate, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </af-table-column> -->
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>
<script>
import {
  listOrder,
  exportOrder,
  exportLngOrderGroup,
  exportLngOrderStation,
  queryGroupOrder,
  queryStationOrder,
  refund,
  reback,
} from "@/api/station/order";
import { stationinfo } from "@/api/station/gun";
import { listPrice, getPrice } from "@/api/station/price";
import { listSum, listAllSum } from "@/api/dataSource/saleReport";

export default {
  name: "Order_Lng",
  data() {
    return {
      //创建订单时间间隔
      dateRangeCreatedDate: [],
      // 总条数
      total: 0,
      // 订单支付表格数据
      orderList: [],
      oilNameOptions: [],
      // 字典
      payTypeOptions: [],
      // 下拉油站
      stationOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        levelId: this.levelId,
        pageNum: 1,
        pageSize: 10,
        oilGun: null,
        oilName: null,
        stationId: null,
        orderType: 1,
        oilPersonnel: null,
        createdDate: null,
        likeConsumer: null,
        orderNo: null,
      },
      // 表单校验
      rules: {},
      lngData: {},
    };
  },
  created() {
    this.getList();
    //字典
    this.getDicts("pay_type").then((response) => {
      this.payTypeOptions = response.data;
    });
    stationinfo().then((response) => {
      this.stationOptions = response.rows;
    });
    this.getDicts("oil_name").then((response) => {
      this.oilNameOptions = response.data;
    });
  },
  methods: {
    payTypeFotmat(row, column) {
      if (row.payType === "wx") {
        return "微信支付";
      } else if (row.payType === "zfb") {
        return "支付宝";
      } else if (row.payType === "xj") {
        return "现金支付";
      } else if (row.payType === "dzk") {
        return "电子卡支付";
      } else if (row.payType === "POS") {
        return "POS机支付";
      } else if (row.payType === "yzf") {
        return "预支付";
      } else if (row.payType === "kbzf") {
        return "卡包支付";
      }
    },
    statusFotmat(row, column) {
      if (row.status === "0") {
        return "未支付";
      } else if (row.status === "1") {
        return "已支付";
      } else if (row.status === "2") {
        return "线下退款";
      } else if (row.status === "3") {
        return "线上退款";
      }
    },
    /** 查询订单支付列表 */
    getList() {
      this.queryParams.levelId = this.levelId;
      let arrPro;
      const query = {
        stationId: null,
        createdDate: null,
        stationName: null,
        beginTime: null,
        endTime: null,
        levelId: this.levelId,
      };
      if (this.jiBie == 1) {
        arrPro = queryGroupOrder(
          this.addDateRange(this.queryParams, this.dateRangeCreatedDate)
        );
        query.beginTime = this.dateRangeCreatedDate[0];
        query.endTime = this.dateRangeCreatedDate[1];
        listSum(query)
          .then((response) => {
            if (response.data != null) {
              this.lngData = response.data;
            } else {
              throw new Error("");
            }
          })
          .catch(() => {
            this.msgError("拉取overview不同油品的数据失败");
          });
      } else if (this.jiBie == 2) {
        arrPro = queryStationOrder(
          this.addDateRange(this.queryParams, this.dateRangeCreatedDate)
        );
        query.beginTime = this.dateRangeCreatedDate[0];
        query.endTime = this.dateRangeCreatedDate[1];
        listSum(query)
          .then((response) => {
            if (response.data != null) {
              this.lngData = response.data;
            } else {
              throw new Error("");
            }
          })
          .catch(() => {
            this.msgError("拉取overview不同油品的数据失败");
          });
      } else {
        arrPro = queryGroupOrder(
          this.addDateRange(this.queryParams, this.dateRangeCreatedDate)
        );
        listAllSum(query)
          .then((response) => {
            if (response.data != null) {
              this.lngData = response.data;
            } else {
              throw new Error("");
            }
          })
          .catch(() => {
            this.msgError("拉取overview不同油品的数据失败");
          });
      }
      arrPro.then((response) => {
        this.orderList = response.rows;
        this.total = response.total;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRangeCreatedDate = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 导出按钮操作 */
    handleExport() {
      const that = this;
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有订单支付数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          if (that.jiBie == 1) {
            return exportLngOrderGroup(queryParams);
          } else if (that.jiBie == 2) {
            return exportLngOrderStation(queryParams);
          }
        })
        .then((response) => {
          this.download(response.msg);
        });
    },
    //退款方法接口
    handlerefund(row) {
      this.$confirm('是否进行退款"', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return refund(row.orderId);
        })
        .then((res) => {
          // this.getList();
          this.getList();
          this.msgSuccess("退款成功");
        });
    },
    //回退方法
    handlereback(row) {
      this.$confirm('是否对退款账单进行回退"', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return reback(row.orderId);
        })
        .then((res) => {
          // this.getList();
          this.getList();
          this.msgSuccess("回退成功");
        });
    },
  },
};
</script>
